package com.chinaunicom.member.trigger.http;

import com.chinaunicom.common.result.JsonResult;
import com.chinaunicom.member.domain.model.entity.UserLoginEntity;
import com.chinaunicom.member.domain.service.MemberService;
import com.chinaunicom.member.trigger.http.vo.UserLoginVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author guomenghuai
 * @date 2024/1/8
 * @Description
 */
@RestController
@RequestMapping("/member")
public class LoginController {

    @Resource
    private MemberService memberService;

    @PostMapping("/login")
    public JsonResult<String> login(@RequestBody @Valid UserLoginVO userLogin, HttpServletResponse httpResponse) {
        UserLoginEntity userLoginEntity = UserLoginEntity.builder()
                .username(userLogin.getUsername())
                .password(userLogin.getPassword())
                .build();
        String token = memberService.login(userLoginEntity);
        if (StringUtils.isNotBlank(token)) {
            return JsonResult.success(token);
        }
        return JsonResult.ok();
    }

}
